package com.wtwd.campus.jdzc.service.impl;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.CommandRecord;
import com.wtwd.campus.jdzc.dao.JdDeviceInfoMapper;
import com.wtwd.campus.jdzc.dao.JdzcCommandRecordMapper;
import com.wtwd.campus.jdzc.entity.DeviceInfo;
import com.wtwd.campus.jdzc.service.JdzcDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mjy
 * @date 2022/02/15
 * @description
 */
@Service
public class JdzcDeviceInfoServiceImpl implements JdzcDeviceInfoService {

    @Autowired
    private JdDeviceInfoMapper jdDeviceInfoMapper;

    @Autowired
    private JdzcCommandRecordMapper jdzcCommandRecordMapper;


    @Override
    public DeviceInfo getDeviceInfoByImei(String imei) {
        return jdDeviceInfoMapper.getDeviceInfoByImei(imei);
    }

    @Override
    public int updateDeviceInfo(DeviceInfo deviceInfo) {
        return jdDeviceInfoMapper.updateDeviceInfo(deviceInfo);
    }

    @Override
    public List<DeviceInfo> getDeviceInfoListByStuIdList(List<Integer> list) {
        return jdDeviceInfoMapper.getDeviceInfoListByStuIdList(list);
    }

    @Override
    public DeviceInfo getDeviceInfoByStuId(Integer studentId) {
        return jdDeviceInfoMapper.getDeviceInfoByStuId(studentId);
    }

    @Override
    public int unBindingStudentWithDevice(Integer studentId) {
        return jdDeviceInfoMapper.unBindingStudentWithDevice(studentId);
    }

    @Override
    public List<Map<String, Object>> getStudentPlaceByStudentId(List<Integer> list) {
        return jdDeviceInfoMapper.getStudentPlaceByStudentId(list);
    }

    @Override
    public DeviceInfo getDeviceInfoByDeviceId(Integer deviceId) {
        return jdDeviceInfoMapper.getDeviceInfoByDeviceId(deviceId);
    }

    @Override
    public Map<String, Object> getModeStatusByDeviceId(Integer deviceId, String imei) {
      /*  //根据imei获取设备设置信息
        DeviceSetting deviceSetting = deviceSettingMapper.getDeviceSettingByDeviceId(deviceId);
        Integer sleepSwitch = deviceSetting.getSleepSwitch();
        Integer silentShutdownSwitch = deviceSetting.getSilentShutdown();
        Map<String, Object> map = new HashMap<>();
        map.put("sleepMode", sleepSwitch == null ? Constants.SLEEP_SWITCH_CLOSE : sleepSwitch);
        map.put("shutDownSilentMode", silentShutdownSwitch == null ? Constants.SILENT_SHUTDOWN_CLOSE : silentShutdownSwitch);*/
        List<String> typeList = new ArrayList<>();
        typeList.add(Constants.SET_WORK_MODE);
        typeList.add(Constants.AUTO_CHANGE_WORK_MODE);
        typeList.add(Constants.SHUT_DOWN_SILENT);
        //根据imei获取定位模式、睡眠模式的最后一条记录
        List<CommandRecord> list = jdzcCommandRecordMapper.getSpecialRecordByImei(imei, typeList);
        Map<String, Object> map = new HashMap<>();
        for (CommandRecord record : list) {
            String commandType = record.getCommandType();
            Integer result = record.getResult();
            if (Constants.SET_WORK_MODE.equals(commandType)) {
                map.put("locateMode", result);
            } else if (Constants.AUTO_CHANGE_WORK_MODE.equals(commandType)) {
                map.put("sleepMode", result);
            } else {
                map.put("shutDownSilentMode", result);
            }
        }
        //如果没有下发过指令，则直接返回成功
        if (!map.containsKey("locateMode")) {
            map.put("locateMode", Constants.COMMAND_COMPLETE);
        }
        if (!map.containsKey("sleepMode")) {
            map.put("sleepMode", Constants.COMMAND_COMPLETE);
        }
        if (!map.containsKey("shutDownSilentMode")) {
            map.put("shutDownSilentMode", Constants.COMMAND_COMPLETE);
        }
        return map;
    }


}
